package com.forlove.app.qselect.friend.presenter;

import android.text.TextUtils;

import com.forlove.app.common.base.mvp.presenter.IBasePresenter;
import com.forlove.app.common.network.ICallBack;
import com.forlove.app.common.utils.ParseHelper;
import com.forlove.app.manager.NetworkManager;
import com.forlove.app.manager.ServerManager;
import com.forlove.app.qselect.CommonEntity;
import com.forlove.app.qselect.friend.entity.SearchFriendEntity;
import com.forlove.app.qselect.friend.service.IAddNewFriendService;
import com.forlove.app.qselect.friend.service.impl.AddNewFriendServiceImpl;
import com.forlove.app.qselect.friend.view.AddNewFriendView;

import java.util.Map;

/**
 * Created by yjl on 2017/10/23.
 */

public class AddNewFriendPresenter extends IBasePresenter {
    private AddNewFriendView addNewFriendView;
    private IAddNewFriendService addNewFriendService;

    public AddNewFriendPresenter(AddNewFriendView addNewFriendView) {
        this.addNewFriendView = addNewFriendView;
        if (addNewFriendService == null) {
            addNewFriendService = new AddNewFriendServiceImpl();
            setService(addNewFriendService);
        }
    }

    public void loadSearchFriend(Map param) {
        addNewFriendService.loadData(ServerManager.ACTION_FRIEND_SEARCH, param, new ICallBack() {
            @Override
            public void onSuccess(int statusCode, String result) {
                SearchFriendEntity searchFriendEntity = ParseHelper.getModel(result, "data", SearchFriendEntity.class);
                if (addNewFriendView != null) {
                    addNewFriendView.showScanViewData(searchFriendEntity);
                }
            }

            @Override
            public void onFail(int statusCode, String result) {
                CommonEntity commonEntity = new CommonEntity();
                commonEntity.parseJSONString(result);
                if (!TextUtils.isEmpty(commonEntity.message)) {
                    if (addNewFriendView != null) {
                        addNewFriendView.showErrorData(NetworkManager.ResultCode.RESULT_FAIL, commonEntity.message);
                    }
                } else {
                    if (addNewFriendView != null) {
                        addNewFriendView.showErrorData(NetworkManager.ResultCode.RESULT_ERROR, "稍后重试!");
                    }
                }
            }
        });
    }
}
